package com.study.d2_reflect;

import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;

//目标：保存任意对象的字段和其他数据到文件中去
public class ObjectFrame {
    //但是这个对象有多少个字段，我们不清楚，说清楚呢？只有反射
    public  static void saveObject(Object o)throws Exception{
        PrintStream ps = new PrintStream(new FileOutputStream("day12/src/obj.txt",true));

        Class c = o.getClass();
        String name = c.getSimpleName();
        ps.println("==="+name+"===");
        Field[] declaredFields = c.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            String n = declaredField.getName();
            String value = declaredField.get(o)+"";
            ps.print(n+"="+value+"\n");
        }
        ps.close();
    }
}
